package com.yonyou.ucf.mdf.app.isvdxq.firstpart.guize.controller;

import com.google.gson.Gson;
import com.yonyou.ucf.mdf.app.isvdxq.ReturnResult.DataResult;
import com.yonyou.ucf.mdf.app.isvdxq.common.DxqConfig;
import com.yonyou.ucf.mdf.app.isvdxq.common.NoNeedToken;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.guize.entity.OrdernoticeView;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.guize.service.GuiZeService;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.entity.OrderNoticeEntity;
import com.yonyou.ucf.mdf.app.isvdxq.other.service.BaseService;
import com.yonyou.ucf.mdf.app.isvdxq.utils.RequestTool;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

//@RestController=@Controller+@ResposeBody,返回JSON字符串。但不能解析html或jsp视图，不能返回jsp和html页面
@RestController
@RequestMapping(value = {DxqConfig.pre_apiUrl+"/guize",DxqConfig.pre_pcUrl+"/guize"})
public class GuiZeController {
    private static final Logger logger = LoggerFactory.getLogger(GuiZeController.class);

    @Autowired
    private GuiZeService guizeService;

    @Autowired
    private BaseService baseService;

    //弃审等插入数据 暂未使用
    @PostMapping("/Insertordernotice")
    public DataResult Insertordernotice(@RequestBody List<OrderNoticeEntity> ordernoticeList, HttpServletRequest request) throws IOException {
        Map<String, Object> params = RequestTool.getParams(request);
        String tenantId = baseService.getTenantId(request);
        String userId = RequestTool.getHeaderAndParameter(request, DxqConfig.DXQ_userId);
        String ordertype = "3"; //到货单
        return guizeService.InsertorUpdateordernotice(ordernoticeList, tenantId, ordertype);
    }

    //到货单弃审处理
    @RequestMapping("/Saveordernotice")
    public DataResult Saveordernotice(@RequestBody OrdernoticeView ordernotice, HttpServletRequest request) throws IOException {
        Map<String, Object> params = RequestTool.getParams(request);
        String tenantId = baseService.getTenantId(request);
        String userId = RequestTool.getHeaderAndParameter(request, DxqConfig.DXQ_userId);
        DataResult result = new DataResult(0, "操作失败");
        try {
            String ordertype = "3"; //到货单
            result = guizeService.Saveordernotice(ordernotice, ordertype, tenantId);
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
        return result;
    }

    //发货单弃审
    @RequestMapping("/SalesDeliveryAbandonAudit")
    public DataResult SalesDeliveryAbandonAudit(String idsString, HttpServletRequest request) throws Exception {
        Map<String, Object> params = RequestTool.getParams(request);
        String tenantId = baseService.getTenantId(request);
        String userId = RequestTool.getHeaderAndParameter(request, DxqConfig.DXQ_userId);
        if (params != null) {
            idsString = (String) params.get("idsString");
        }
        // 需要将对应的 ordernotice 状态重置
        DataResult result = guizeService.SalesDeliveryAbandonAudit(idsString, tenantId);
        logger.info("[SalesUnAudit]" + new Gson().toJson(result));
        return result;
    }

    // 发货单审核
    @RequestMapping("/SalesDeliveryAudit")
    public DataResult SalesDeliveryAudit(String idsString, HttpServletRequest request) throws Exception {
        Map<String, Object> params = RequestTool.getParams(request);
        String tenantId = baseService.getTenantId(request);
        String userId = RequestTool.getHeaderAndParameter(request, DxqConfig.DXQ_userId);
        String flag_needaudit = request.getParameter("flagaudit");
        int flagaudit = 0;
        if (!StringUtils.isEmpty(flag_needaudit)) {
            flagaudit = Integer.parseInt(flag_needaudit);
        }

        return guizeService.SalesDeliveryAudit(idsString, tenantId, flagaudit);
    }

    @RequestMapping("/GetOrderNoticeInfo")
    public DataResult GetOrderNoticeInfo(String orderId, String orderType, String tenant_id) {
        return guizeService.SelectOrderAuditStatus(orderId, orderType);
    }

    //根据appcode获取服务列表
    @RequestMapping("/Getlistbyappcode")
    public DataResult Getlistbyappcode(HttpServletRequest request) throws IOException {
        String tenantId = baseService.getTenantId(request);
        String userId = RequestTool.getHeaderAndParameter(request, DxqConfig.DXQ_userId);
        Map<String, Object> params = RequestTool.getParams(request);
        String appCode = request.getParameter("appCode");

        return guizeService.Getlistbyappcode(appCode, tenantId);
    }

    //主组织权限查询
    @RequestMapping("/GetmainOrgPermission")
    public DataResult GetmainOrgPermission(String userId, String serviceCode, String tenant_id) {
        return guizeService.GetmainOrgPermission(userId, serviceCode, tenant_id);
    }

    /**
     * 取出当前登录页面
     * /gdgy/api/isv2/guize/GetBaseInfo?code=
     * @param code
     * @return
     */
    @NoNeedToken
    @RequestMapping("/GetBaseInfo")
    public DataResult GetBaseInfo(String code, String suiteKey, String suiteSecret) {
        if (StringUtils.isEmpty(suiteKey) && StringUtils.isEmpty(suiteSecret)) {
            return baseService.GetBaseInfo(code);
        } else {
            return baseService.GetBaseInfo(code, suiteKey, suiteSecret);
        }
    }
}